/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CloseTournamentDateViewImpl.java
 *
 * Created on 13/02/2010, 20:41:37
 */

package tpdisenio2009.mvp.display;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import tpdisenio2009.model.beans.SoccerMatch;
import tpdisenio2009.model.beans.TournamentDate;

/**
 *
 * @author DeuterioX
 */
public class CloseTournamentDateViewImpl extends javax.swing.JPanel implements CloseTournamentDateView {

    private List<SoccerMatch> soccerMatches = null;

    /** Creates new form CloseTournamentDateViewImpl */
    public CloseTournamentDateViewImpl() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableMatchesForDate = new javax.swing.JTable();
        jLabelDateLabel = new javax.swing.JLabel();
        jComboDate = new javax.swing.JComboBox();
        jButtonCloseDate = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableMatchesForDate.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Partido Numero", "Equipo Local", "Equipo Visitante", "Estado del Partido"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableMatchesForDate.setName("jTableMatchesForDate"); // NOI18N
        jScrollPane1.setViewportView(jTableMatchesForDate);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tpdisenio2009.Tpdisenio2009App.class).getContext().getResourceMap(CloseTournamentDateViewImpl.class);
        jTableMatchesForDate.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableMatchesForDate.columnModel.title0")); // NOI18N
        jTableMatchesForDate.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableMatchesForDate.columnModel.title1")); // NOI18N
        jTableMatchesForDate.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableMatchesForDate.columnModel.title2")); // NOI18N
        jTableMatchesForDate.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTableMatchesForDate.columnModel.title3")); // NOI18N

        jLabelDateLabel.setText(resourceMap.getString("jLabelDateLabel.text")); // NOI18N
        jLabelDateLabel.setName("jLabelDateLabel"); // NOI18N

        jComboDate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboDate.setName("jComboDate"); // NOI18N

        jButtonCloseDate.setText(resourceMap.getString("jButtonCloseDate.text")); // NOI18N
        jButtonCloseDate.setName("jButtonCloseDate"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelDateLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 463, Short.MAX_VALUE)
                        .addComponent(jButtonCloseDate)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDateLabel)
                    .addComponent(jComboDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCloseDate))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCloseDate;
    private javax.swing.JComboBox jComboDate;
    private javax.swing.JLabel jLabelDateLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableMatchesForDate;
    // End of variables declaration//GEN-END:variables

    public void setDates( List<TournamentDate> dates) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for ( TournamentDate date : dates ) {
            model.addElement( date );
        }
        jComboDate.setModel( model );
    }

    public TournamentDate getDate() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) jComboDate.getModel();
        return (TournamentDate) model.getSelectedItem();
    }

    public void setState( List<String> states ) {
        TableColumn stateColumn = jTableMatchesForDate.getColumnModel().getColumn( 3 );
        JComboBox stateComboBox = new JComboBox();
        for ( String state : states ) {
            stateComboBox.addItem( state );
        }
        stateColumn.setCellEditor( new DefaultCellEditor( stateComboBox ) );
    }

    public void loadMatches( List<SoccerMatch> matches ) {
        DefaultTableModel model = (DefaultTableModel) jTableMatchesForDate.getModel();
        model.setRowCount( 0 );
        int matchNumber = 0;
        for ( SoccerMatch match : matches ) {
            String values[] = new String[4];
            values[0] = String.valueOf( matchNumber++ );
            values[1] = match.getRealTeamLocal().getName();
            values[2] = match.getRealTeamVisitor().getName();
            values[3] = match.getState();
            model.addRow( values );
        }
        jTableMatchesForDate.setModel( model );
        soccerMatches = matches;
    }

    public List<SoccerMatch> getSoccerMatch() {
        DefaultTableModel model = (DefaultTableModel) jTableMatchesForDate.getModel();
        for ( int i = 0; i <= model.getRowCount() - 1; i++ ) {
            soccerMatches.get( i ).setState( model.getValueAt( i, 3 ).toString() );
        }
        return soccerMatches;
    }


    public void showError( String errorMessage, String titleMessageWindow ) {
        JOptionPane.showMessageDialog( this,
                errorMessage,
                titleMessageWindow,
                JOptionPane.ERROR_MESSAGE);
    }

    public void showNotification( String notifiactionMessage, String notificationMessageTitle ) {
        JOptionPane.showMessageDialog( this,
                notifiactionMessage,
                notificationMessageTitle,
                JOptionPane.INFORMATION_MESSAGE);
    }

    public void addDateChangeListener( ItemListener listener ) {
        jComboDate.addItemListener( listener );
    }

    public void removeDateChangeListener( ItemListener listener ) {
        jComboDate.removeItemListener( listener );
    }

    public void addCloseDateListener( ActionListener listener ) {
        jButtonCloseDate.addActionListener( listener );
    }

    public void removeCloseDateListener( ActionListener listener ) {
        jButtonCloseDate.removeActionListener( listener );
    }

    public void setFirstOpenDate( TournamentDate date ) {
        jComboDate.getModel().setSelectedItem(date);
    }

    public boolean showConfirmation( String notifiactionMessage ) {
        int confirm = JOptionPane.showConfirmDialog( this, notifiactionMessage, "Confirmación", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );

        if (JOptionPane.YES_OPTION == confirm)
            return true;
        else
            return false;
    }

    public JComponent asWidget() {
        return this;
    }
}
